package com.sandking.db.bean;

import com.sandking.tools.SK_Map;
import java.util.ArrayList;
import com.sandking.db.cache.YhCache;
import com.alibaba.fastjson.JSON;
import java.util.Map;
import com.sandking.io.SK_OutputStream;
import com.sandking.db.cache.XdjlCache;
import java.util.List;
import com.sandking.io.SK_InputStream;
import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.io.ByteArrayOutputStream;

/**
 * 寻敌记录
 */
public class Xdjl {

	public static final String TABLENAME = "寻敌记录";
	public static final String CLASSNAME = "Xdjl"; 
	/**
	 * 需要更新字段集
	 */
	private Map<String, Object> updateColumns;

	/** id */
	private int id;
	
	/** 用户id */
	private int yhid;
	
	/** 目标id */
	private int mbid;
	
	/** 寻敌时间 */
	private java.util.Date xdsj;
	
	
	public Xdjl() {
		super();
	}
	
	public Xdjl(int id, int yhid, int mbid, java.util.Date xdsj) {
		super();
		this.id = id;
		this.yhid = yhid;
		this.mbid = mbid;
		this.xdsj = xdsj;
	}
	
	public Map<String, Object> getUpdateColumns() {
		if(updateColumns == null)
			updateColumns = new HashMap<String, Object>();
		return updateColumns;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
		addUpdateColumn("id",id);
	}
	
	public void changeIdWith(int id){
		this.id += id;
		setId(this.id);
	}
	
	public void changeIdWithMin(int id,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMax(int id,int max){
		this.id += id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMaxMin(int id,int max,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}	
	public int getYhid() {
		return yhid;
	}
	
	public void setYhid(int yhid) {
		this.yhid = yhid;
		addUpdateColumn("用户id",yhid);
	}
	
	public void changeYhidWith(int yhid){
		this.yhid += yhid;
		setYhid(this.yhid);
	}
	
	public void changeYhidWithMin(int yhid,int min){
		this.yhid += yhid;
		this.yhid = this.yhid < min ? min : this.yhid;
		setYhid(this.yhid);
	}
	
	public void changeYhidWithMax(int yhid,int max){
		this.yhid += yhid;
		this.yhid = this.yhid > max ? max : this.yhid;
		setYhid(this.yhid);
	}
	
	public void changeYhidWithMaxMin(int yhid,int max,int min){
		this.yhid += yhid;
		this.yhid = this.yhid < min ? min : this.yhid;
		this.yhid = this.yhid > max ? max : this.yhid;
		setYhid(this.yhid);
	}	
	public int getMbid() {
		return mbid;
	}
	
	public void setMbid(int mbid) {
		this.mbid = mbid;
		addUpdateColumn("目标id",mbid);
	}
	
	public void changeMbidWith(int mbid){
		this.mbid += mbid;
		setMbid(this.mbid);
	}
	
	public void changeMbidWithMin(int mbid,int min){
		this.mbid += mbid;
		this.mbid = this.mbid < min ? min : this.mbid;
		setMbid(this.mbid);
	}
	
	public void changeMbidWithMax(int mbid,int max){
		this.mbid += mbid;
		this.mbid = this.mbid > max ? max : this.mbid;
		setMbid(this.mbid);
	}
	
	public void changeMbidWithMaxMin(int mbid,int max,int min){
		this.mbid += mbid;
		this.mbid = this.mbid < min ? min : this.mbid;
		this.mbid = this.mbid > max ? max : this.mbid;
		setMbid(this.mbid);
	}	
	public java.util.Date getXdsj() {
		return xdsj;
	}
	
	public void setXdsj(java.util.Date xdsj) {
		this.xdsj = xdsj;
		addUpdateColumn("寻敌时间",xdsj);
	}
	
	
	//用户id
	public Yh getYhPkYhid(){
		return YhCache.getById(yhid);
	}
	//目标id
	public Yh getYhPkMbid(){
		return YhCache.getById(mbid);
	}
	public void addUpdateColumn(String key, Object val) {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.put(key, val);
	}
	
	public void clearUpdateColumn() {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.clear();
	}
	
	public Map<String, Object> toMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("yhid", this.yhid);
        result.put("mbid", this.mbid);
        result.put("xdsj", this.xdsj);
        return result;
    }
    
    public String toString(){
        return toMap().toString();
    }
    
    public String toJson(){
    	return JSON.toJSONString(toMap());
    }
    
    /**
     * 数据库源字段Map
     */
    public Map<String, Object> toColumnNameMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("用户id", this.yhid);
        result.put("目标id", this.mbid);
        result.put("寻敌时间", this.xdsj);
        return result;
    }
    
    public String toColumnNameString(){
        return toColumnNameMap().toString();
    }
    
    public byte[] toBytes() throws Exception {
    	try (ByteArrayOutputStream out = new ByteArrayOutputStream();) {
		    SK_OutputStream.writeInt(out,this.id);
		    SK_OutputStream.writeInt(out,this.yhid);
		    SK_OutputStream.writeInt(out,this.mbid);
		    return out.toByteArray();
    	}catch (Exception e) {
            throw e;
        }
    }
    
     public static Xdjl createForBytes(byte[] _byte) throws Exception {
     	try (ByteArrayInputStream in = new ByteArrayInputStream(_byte);) {
	     	Xdjl xdjl = new Xdjl();
		    xdjl.id = SK_InputStream.readInt(in,null);
		    xdjl.yhid = SK_InputStream.readInt(in,null);
		    xdjl.mbid = SK_InputStream.readInt(in,null);
		    return xdjl;
    	}catch (Exception e) {
            throw e;
        }
     }
    
    /**
     * 根据list创建对象
     */
    public static List<Xdjl> createForColumnNameList(List<Map<String, Object>> list){
    	List<Xdjl> xdjls = new ArrayList<Xdjl>();
		for (Map<String, Object> map : list) {
			xdjls.add(createForColumnNameMap(map));
		}
		return xdjls;
    }
    
    /**
     * 根据map创建对象
     */
    public static Xdjl createForColumnNameMap(Map<String, Object> map){
    	Xdjl obj = new Xdjl();
	    obj.id = SK_Map.getInt("id", map);
	    obj.yhid = SK_Map.getInt("用户id", map);
	    obj.mbid = SK_Map.getInt("目标id", map);
        return obj;
    }
    
    /**
     * 根据list创建对象
     */
    public static List<Xdjl> createForList(List<Map<String, Object>> list){
    	List<Xdjl> xdjls = new ArrayList<Xdjl>();
		for (Map<String, Object> map : list) {
			xdjls.add(createForColumnNameMap(map));
		}
		return xdjls;
    }
    
    /**
     * 根据map创建对象
     */
    public static Xdjl createForMap(Map<String, Object> map){
    	Xdjl obj = new Xdjl();
	    obj.id = SK_Map.getInt("id", map);
	    obj.yhid = SK_Map.getInt("yhid", map);
	    obj.mbid = SK_Map.getInt("mbid", map);
        return obj;
    }
    
    public static Xdjl createForJson(String json){
    	Map<String, Object> map = JSON.parseObject(json);
    	return createForMap(map);
    }
    
    public static List<Xdjl> createForJson(List<String> jsons){
    	List<Xdjl> xdjls = new ArrayList<Xdjl>();
    	for(String json : jsons){
    		xdjls.add(createForJson(json));
    	}
    	return xdjls;
    }
    
    /** 延迟插入数据库 */
    public Xdjl insert(){
    	return XdjlCache.insert(this);
    }
    /** 延迟更新数据库 */
    public Xdjl update(){
    	return XdjlCache.update(this);
    }
    /** 延迟删除数据库 */
    public boolean delete(){
    	return XdjlCache.delete(this);
    }
    /** 即时插入数据库 */
    public Xdjl insertAndFlush(){
    	return XdjlCache.insertAndFlush(this);
    }
    /** 即时更新数据库 */
    public Xdjl updateAndFlush(){
    	return XdjlCache.updateAndFlush(this);
    }
    /** 即时删除数据库 */
    public boolean deleteAndFlush(){
    	return XdjlCache.deleteAndFlush(this);
    }
}